package com.workman.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workman.common.util.MD5;
import com.workman.common.util.RandomUtil;
import com.workman.common.util.WorkManConstans;
import com.workman.dao.mapper.UserMapper;
import com.workman.factory.PageFactory;
import com.workman.factory.UserFactory;
import com.workman.model.User;
import com.workman.model.dto.UserDto;
import com.workman.model.exception.ApplicationException;
import com.workman.model.exception.ApplicationExceptionEnum;
import com.workman.model.vo.front.Results;
import com.workman.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController()
@RequestMapping(value = "/user")
@Api(value = "用户", tags = "用户相关接口")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    public Object list(@RequestParam(required = false) String loginName,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer sex) {
        Page page = new PageFactory().defaultPage();

        QueryWrapper<UserMapper> userMapperQueryWrapper = new QueryWrapper<>();
        userMapperQueryWrapper.eq("login_name", loginName);
        userMapperQueryWrapper.like("name", name);
        userMapperQueryWrapper.eq("status", 0);
        userMapperQueryWrapper.eq("sex", sex);

        IPage<User> iPage = userService.selectUserByPage(page, userMapperQueryWrapper);
        return iPage;
    }


    @RequestMapping(method = RequestMethod.POST, value = "/save")
    @ApiOperation(value = "新增或修改用户", tags = "新增或修改用户")
    public Object save(UserDto userDto) {
        if (userDto.getId() == null) {//新增
            //TODO check user info
            User alreayUser = userService.selectUserByLoginName(userDto.getLoginName());
            if (alreayUser != null) {
                throw new ApplicationException(ApplicationExceptionEnum.USER_ALREADY_REG);
            }
            if (StringUtils.isBlank(userDto.getPassword())) {
                return Results.failure("密码不能为空！");
            }
            User user = UserFactory.createUser(userDto, new User());
            user.setSalt(RandomUtil.String(5));
            user.setPassword(MD5.md5(userDto.getPassword(), user.getSalt()));
            user.setStatus(WorkManConstans.USER_STATUS_NORMAL);
            userService.insertUser(user);
            return Results.success();
        }

        //修改
        User oldUser = userService.selectUserById(userDto.getId());
        userService.updateUserById(UserFactory.updateUser(userDto, oldUser));
        return Results.success();
    }

}
